package com.hxn.manage;

import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import javax.xml.crypto.Data;
import java.util.function.Consumer;

public class MybatisPlusGenerator {

    //数据源
    //private static final String URL = "jdbc:mysql://192.168.10.117:3306/testplus?useUnicode=true&characterEncoding=utf8";
    private static final String URL = "jdbc:mysql://localhost:3306/lottery_ticket?useUnicode=true&characterEncoding=utf8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    /*//全局配置
    //作者
    private static final String AUTHOR = "gl";
    //是否需要配置Swagger注解
    private static final Boolean ISSWAGGER = true;
    //注释时间的时间格式
    private static final String COMMENTDATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    //包配置
    //代码生成在哪个包下
    private static final String PARENT_PACKAGE = "com.hxn.manage";
    //实体包名
    private static final String ENTITY_PACKAGE = "entity";
    //dao接口包名
    private static final String DAO_PACKAGE = "dao";
    //service层包名
    private static final String SERVICE_PACKAGE = "service";
    //控制器包名
    private static final String CONTROL_PACKAGE = "controller";
    //映射文件包名
    private static final String MAPPER_PACKAGE = "mapper";

    //策略配置
    //是否生成所有表
    private static final Boolean ALL_TABLE = false;
    //针对哪些表要生成,此属性生效的前提是上一项属性(ALL_TABLE)为false
    private static final String [] TABLE_ARRAY = {"sys_dept","sys_user","sys_role"};
    //生成时要去除的表名前缀
    private static final String [] PREFIX_TABLE = {"sys_"};
    //entity策略配置
    //是否添加lombok注解
    private static final Boolean LOMBOK_ANNOTATION = true;
    //是否给所有属性加上@TableField注解
    private static final Boolean TABLEFIELD_ANNOTATION = true;
    //创建时间自动填充，这里，数据库表中创建时间写死了叫create_time
    private static final Boolean FILL_CREATE_TIME = true;
    //更新时间自动填充，表字段名为update_time
    private static final Boolean FILL_UPDATE_TIME = true;
    //service层策略配置
    //service接口名字格式化
    private static final String FORMAT_SERVICE_NAME = "%sService";
    //service接口继承父类,默认为com.baomidou.mybatisplus.extension.service.IService
    private static final String PARENT_SERVICE = "com.github.yulichang.base.MPJBaseService";
    //service实现类需要继承的父类,默认为com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
    private static final String PARENT_SERVICE_IMPL = "com.github.yulichang.base.MPJBaseServiceImpl";
    //dao层策略配置
    //dao接口名字格式化
    private static final String FORMAT_DAO_NAME = "%sDao";
    //dao接口继承父类,默认为com.baomidou.mybatisplus.extension.service.IService
    private static final String PARENT_DAO = "com.baomidou.mybatisplus.core.mapper.BaseMapper";
    //dao接口是否添加@Mapper注解
    private static final Boolean MAPPER_ANNOTATION = true;
    //是否生成ResultMap结果集
    private static final Boolean BASE_RESULT_MAP = true;
    //controller策略配置
    //是否为控制器类生成@RestController注解,默认为@Controller
    private static final Boolean REST_STYLE = true;*/




    public static void main(String[] args) {

        createBy352();


    }

    /**
     * 3.5.2版本的生成器生成方式
     */
    public static void createBy352(){
        DataSourceConfig.Builder configBuilder = new DataSourceConfig.Builder(URL, USERNAME, PASSWORD);
        FastAutoGenerator generator = FastAutoGenerator.create(configBuilder);
        //全局配置
        generator.globalConfig(builder->{
            String projectPath = System.getProperty("user.dir");// 项目路径
            builder.author("leo")//作者，开发人员
                    .outputDir(projectPath+"/src/main/java")// 生成的文件路径
                    .disableOpenDir()// 是否打开生成的目录
                    .enableSwagger()//实体属性 Swagger2 注解，需要配置Swagger2依赖
                    //.fileOverride()// 是否覆盖已有文件, 默认false
                    .commentDate("yyyy-MM-dd HH:mm:ss")
                    .dateType(DateType.ONLY_DATE).build();
        });

        //包配置
        generator.packageConfig(builder -> {
            builder.parent("com.lottery.www")//父包名，代码所在的包
                    .moduleName(null)//模块名
                    .entity("entity")//实体包名
                    .mapper("dao")//mapper包名
                    .service("service")//service层包名
                    .controller("controller")//控制器包名
                    .xml("mapper");//映射文件包名
        });

        //策略配置
        generator.strategyConfig(builder -> {
            builder.addInclude("userinfo_headers")//哪些表要生成,不写表示所有表
            //builder.addTablePrefix("dc_")//表名前缀要去掉
                    .entityBuilder()//实体策略配置
                    .enableLombok()//加上lombok注解
                    .enableTableFieldAnnotation()//所有属性都加上TableField注解
                    .addTableFills(new Column("create_time", FieldFill.INSERT))//创建时间自动填充
                    .addTableFills(new Column("update_time",FieldFill.INSERT_UPDATE))//更新时间自动填充
                    .serviceBuilder()//service层策略配置
                    .formatServiceFileName("%sService")//service接口名字不要I开头，这里表示以实体类名后面加上Service作为接口名字
                    .superServiceClass("com.github.yulichang.base.MPJBaseService")//service接口继承父类,默认为com.baomidou.mybatisplus.extension.service.IService
                    .superServiceImplClass("com.github.yulichang.base.MPJBaseServiceImpl")//service实现类继承父类,默认为com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
                    .mapperBuilder()//dao层策略配置
                    .formatMapperFileName("%sDao")//dao接口的名字
                    .superClass("com.github.yulichang.base.MPJBaseMapper")//dao接口继承的父类,默认为com.baomidou.mybatisplus.extension.service.IService
                    .enableMapperAnnotation()//dao接口加上mapper注解
                    .enableBaseResultMap()//在xml文件中生成基本的ResultMap结果集
                    .controllerBuilder()//controller策略配置
                    .enableRestStyle();//启用rest风格，controller类前加上RestController注解
        });


        generator.execute();
    }



    /*public static void generateBy340(){
        AutoGenerator generator = new AutoGenerator();

        //数据源
        DataSourceConfig ds = new DataSourceConfig();
        ds.setUrl(URL);
        ds.setUsername(USERNAME);
        ds.setPassword(PASSWORD);
        ds.setDbType(DbType.MYSQL);
        generator.setDataSource(ds);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir"); // 项目路径
        globalConfig.setOutputDir(projectPath+"/src/main/java");// 生成的文件路径

        generator.setGlobalConfig(globalConfig);
    }*/

}
